package com.example.controller;

import com.example.annotation.PassToken;
import com.example.entity.delay.CallCdr;
import com.example.entity.delay.DelayedVo;
import com.example.entity.delay.OrderDelayVO;
import com.example.service.IDelayService;
import com.example.service.IDepartmentService;
import com.example.utils.TestOrder;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.redisson.Redisson;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Date;
import java.util.concurrent.*;
import org.redisson.config.Config;

/**
 * @author ChenXiaoBin
 * on 2019/6/17
 */
@RestController
@RequestMapping("/V1.0/celay")
public class DelayController {
    private Logger LOG = Logger.getLogger(String.valueOf(this.getClass()));


    @Autowired
    private IDelayService delayService;

    @Autowired
    private IDepartmentService departmentService;

    public static Config config = new Config();

    @PutMapping("/update")
    @ApiOperation(value = "java 延迟队列", notes = "java 延迟队列")
    public void putupdate() {
        LOG.info("java延迟队列");
        delayService.delayTest();

    }

    // 负责保存限时订单的队列
    public static DelayQueue<DelayedVo<OrderDelayVO>> delayOrder = new DelayQueue<DelayedVo<OrderDelayVO>>();

    @PutMapping("/delay/comment")
    @ApiOperation(value = "comment", notes = "comment")
    public void delayComment() {
        OrderDelayVO orderDelayVO = new OrderDelayVO();
        orderDelayVO.setId(1);
        orderDelayVO.setName("测试");
        DelayedVo<OrderDelayVO> delayedOrder = new DelayedVo<OrderDelayVO>(5000, orderDelayVO);
        // 将订单推入队列
        delayOrder.offer(delayedOrder);
        System.out.println(new Date().toString() + " .....c存消息队列中");
    }


    @PutMapping("/delay/redissionDelay")
    @ApiOperation(value = "redission延时队列", notes = "redission延时队列")
    @PassToken
    public void redissionDelay() {
        departmentService.delay();
    }

}
